package com.skysail.controller;
import com.skysail.common.oauth.auth.SysUserAuthentication;
import com.skysail.model.primary.UserModel;
import com.skysail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@RestController
@RequestMapping("/user")
public class HelloController {
    @Autowired
    private UserService userService;


    @GetMapping("/save")
    public String saveUser() {
           UserModel userModel = new UserModel();
           userModel.setAddress("xxxx");
           userModel.setUserName("yyyyy");
           userModel.setPassword("mmmm");
           userModel.setCreate_by("xxx");
          userService.save(userModel);
        return "添加成功";
    }


    @RequestMapping("/demotest")
    public ModelAndView testthymeleaflist(ModelAndView modelAndView) {
        List<UserModel> listuser = userService.findAll();
        modelAndView.setViewName("testhello");
        modelAndView.addObject("listuser",listuser);
        return modelAndView;
    }

     //AuthenticationPrincipal获取登录用户
//    @RequestMapping("/demotest")
//    public ModelAndView testthymeleaflist(ModelAndView modelAndView, @AuthenticationPrincipal SysUserAuthentication pi) {
//        List<UserModel> listuser = userService.findAll();
//        modelAndView.setViewName("testhello");
//        modelAndView.addObject("listuser",listuser);
//        return modelAndView;
//    }

    @RequestMapping("/demo")
    public ModelAndView testthymeleaf(ModelAndView modelAndView) {
        UserModel userModel = userService.findOne("yyyyy");
        modelAndView.setViewName("hello");
        modelAndView.addObject("user", userModel);
        return modelAndView;
    }

//    @RequestMapping(method = RequestMethod.GET,value = "/{id}/select")
//    public UserModel select(@PathVariable("id")int id){
//        return userService.selectById(id);
//    }

//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public ModelAndView goBuy(RequestParam(value = "id",required = false,defaultValue = "0") String code, ModelAndView modelAndView) {
//    }


}
